<script type="text/javascript">
$(function(){
	var pg = App.config.varIndex.ui, pp = pg.params || {}
		, varPl = pg.children('.varPl')
		, varGd = varPl.find('.grid')
		, varAdd = varPl.seek('add')
		, varFind = varPl.seek('find')
		, varKey = varPl.seek('var')
		, appPl = pg.children('.appPl')
		, appGd = appPl.find('.grid')
		, ownsCbx = appPl.seek('ownsOnly')
		, appAdd = appPl.seek('add')
		, appFind = appPl.seek('find')
		, appKey = appPl.seek('key')
		, fca = []; //app code arrary

	function checkApp(){
		appGd.seek('apps').each(function(i, a){
			var me = $(a);
			me.prop('checked', $.inArray(me.val(), fca) >= 0);
		});
	}

	function varApps(rid){
		$.post(App.config.ctx + '/var/app/list.do', {
			varId: rid,
			pageNo: 1,
			pageSize: 9999
		}, function(rs){
			var ro = mac.eval(rs);
			if (ro.success) {
				fca = $.map(ro.data.list, function(o){
					return o.code;
				});
				checkApp();
			} else {
				App.error(el.html(), ro.data);
			}
		});
	}

	varGd.mac('grid', {
		cols : [{
			field: 'group', title : '变量分组', width: 120, sort: true
		},{
			field: 'env', title : '环境', width: 120, sort: true
		},{
			field: 'name', title : '变量名', width: 120, sort: true,
			render: function(r) {
				return $('<span class="a"></span>').click(function(){
					if (App.logon.power('config.var.save')){
						App.open({
							ctx: 'config',
							action: 'varEdit',
							model: true,
							params: r
						}, function(){
							varGd.load();
						});
					}
				}).append(r.name);
			}
		},{
			field: 'value', title : '变量值', width: 120, sort: true,
			render: function(r) {
				return $('<span class="a"></span>').click(function(){
					if (App.logon.power('config.var.save')){
						App.open({
							ctx: 'config',
							action: 'varEdit',
							model: true,
							params: r
						}, function(){
							varGd.load();
						});
					}
				}).append(r.value);
			}
		},{
			field: 'id', title : '操作', width: 90,
			render: function(r, tr, gd){
				var a = $('<div></div>');
				if (App.logon.power('config.var.put')) {
					$('<span class="a"></span>').click(function(){
						if (confirm('确定要向相关应用推送这个变量吗？')) {
							$.post(App.config.ctx + '/var/put.do', {
								id: r.id,
								name: r.name,
								env: r.env,
								group: r.group,
								value: r.value
							}, function(rs){
								var ro = mac.eval(rs);
								if (ro.success) {
									App.open({
										ctx: 'config',
										action: 'varResult',
										model: true,
										params: ro.data
									}, function(){
										varGd.load();
									});
								} else {
									mac.alert('推送失败');
								}
							});
						}
					}).append('推送').appendTo(a);
				}
				a.append('&nbsp;&nbsp;')
				if (App.logon.power('config.var.delete')) {
					$('<span class="a"></span>').click(function(){
						if (confirm('确定要删除这个变量吗？')) {
							$.post(App.config.ctx + '/var/delete.do', {
								id: r.id
							}, function(rs){
								varGd.load({ pageNo: 1 });
							});
						}
					}).append('删除').appendTo(a);
				}
				return a;
			}
		}],
		loader: {
			url: App.config.ctx + '/var/list.do',
			params: {
				pageNo: 1, pageSize: 50,
				projId: pp.proj.id
			},
			autoLoad: true
		},
		afterLoad: function(dd){
			appGd.find(':checkbox').removeProp('checked');
		},
		onRowClick: function(){
			var tr = $(this), sr = varGd.selected;
			if (tr.hasClass('selected'))
				return true;
			if (sr)
				sr.removeClass('selected');
			sr = varGd.selected = tr.addClass('selected');
			var rid = varGd.data[sr.attr('name')].id;
			if (ownsCbx.is(':checked')) {
				appGd.load({
					varId: rid
				}, App.config.ctx + '/var/app/list.do');
			} else {
				varApps(rid);
			}
			return true;
		},
		pagerLength: 10
	});

	if (App.logon.power('config.var.save')) {
		varAdd.click(function(){
			App.open({
				ctx: 'config',
				action: 'varEdit',
				title: '新增变量',
				model: true,
				params: {
					projId: pp.proj.id
				}
			}, function(){
				varGd.load();
			});
		}).show();
	}

	varFind.click(function() {
		varGd.load({ key: varKey.val() });
	});
	varKey.keydown(function(e){
		if (e.keyCode == 13) {
			varFind.click();
		}
	});	
	appGd.mac('grid', {
		cols : [{
			field: 'id', title: '<input type="checkbox" name="all" />', width : 24,
			render : function(r, tr, gd) {
				if (App.logon.power('config.var.app.toggle')) {
					var a = $('<input type="checkbox" />').change(function() {
						var b = $(this).is(':checked'), t = b? '添加模块权限':'移除模块权限';
						if (!varGd.selected) {
							mac.alert('请先选择一个变量');
							return; //如果左侧没有用户，则点击无效
						}
						var varId = varGd.data[varGd.selected.attr('name')].id,
							w = mac.wait(Msg.info.waiting, [ t ]);
						$.post(App.config.ctx + '/var/app/toggle.do', {
							varId: varId,
							key: r.id,
							status: b? 1:0
						}, function(rs){
							var ro = mac.eval(rs);
							if(ro.success){
								if (ro.data) {
									fca.push(r.code);
								} else {
									var i = $.inArray(r.code, fca);
									fca.splice(i, 1);
								}
							} else {
								App.error(t, ro.data);
							}
							w.close();
						});
					});
					a.attr({
						name : 'apps',
						value : r.code
					});
				}
				return a;
			}
		}, {
			field: 'code', title : '编码', width: 256, sort: true
		}, {
			field: 'name', title : '名称', width: 256, sort: true
		}],
		loader: {
			url: App.config.ctx + '/app/list.do',
			params: { pageNo: 1, pageSize: 50, projId: pp.proj.id },
			autoLoad: true
		},
		pagerLength: 10,
		afterLoad: function(dd, po, gd){
			var ac = gd.find(':checkbox');
			ac.prop('checked', false);
			if (ownsCbx.is(':checked')) {
				fca = $.map(dd, function(o){
					return o.code;
				});
				ac.prop('checked', 'checked');
			} else if (fca.length) {
				checkApp();
			}
		}
	});
	if (!App.logon.power('app.list')){
		appGd.hide();
	}
	appGd.seek('all').click(function(){
		if (!varGd.selected) {
			/* mac.alert('请先选择一个变量'); */
			return; //如果左侧没有用户，则点击无效
		}
		var varId = varGd.selected.attr('name'),
			fcs, b = $(this).prop('checked'), fa = appGd.seek('apps');
		fa.prop('checked', b);
		fcs = fa.map(function(){
			return $(this).val();
		}).get().join(',');
		$.post(App.config.ctx + '/var/app/toggle.do', {
			varId: varId, //var code
			var: fcs, //app codes
			status: b? 1:0
		}, function(rs){
			var ro = mac.eval(rs);
			if (ro.success) {
				varApps(varId);
			} else {
				App.error(el.html(), ro.data);
			}
		});
	});

	appFind.click(function() {
		appGd.load({ key: appKey.val(), pageNo: 1 });
	});
	appKey.keydown(function(e){
		if (e.varCode == 13)
			appFind.click();
	});
	ownsCbx.click(function() {
		var el = $(this);
		if (el.is(':checked')) {
			if (varGd.selected) {
				var k = varGd.selected.attr('name');
				appGd.load({
					varId: varGd.data[k].id,
					pageNo: 1
				}, App.config.ctx + '/var/app/list.do');
			}
		} else {
			appGd.load({ pageNo: 1 }, App.config.ctx + '/app/list.do');
		}
	});

	function adjust(){
		var p = pg.parent(), h = p.height() - 16, w = p.width() - 16;
		pg.height(h).width(w);
		w = (w - 8) / 2;
		varPl.height(h).width(w);
		appPl.height(h).width(w);
		h -= 64;
		varGd.parent().height(h);
		appGd.parent().height(h);
		varGd.adjust();
		appGd.adjust();
	}
	$(window).resize(adjust);
	adjust();
});
</script>
<div class="varPl">
	<div class="head">
		<button name="add" class="hidden">添加新变量</button>
		<input name="var" type="text" placeholder="请输入变量名" />
		<button name="find">查找</button>
	</div>
	<div class="body">
		<div class="grid"></div>
	</div>
</div>
<div class="appPl">
	<div class="head">
		<input name="var" type="text" placeholder="请输入编码、名称或备注" />
		<button name="find">查找</button>
		<input type="checkbox" name="ownsOnly" />
		<span style="word-break: keep-all">仅显示已有权限</span>
	</div>
	<div class="body">
		<div class="grid"></div>
	</div>
</div>